var oResizeData = {};

function cancel_resize() {
	$('#edit_media_image').css({
		width: oResizeData.width,
		height: oResizeData.height
	});
	
	set_zoom_level();
	
	$('#edit_media_tool_cont').html('');
}

function get_ratio() {
	var iRatio = $('#edit_media_image').width() / $('#edit_media_image').height();
	
	$('#image_resize_ratio').val(iRatio);
}

function image_resize() {
	create_overlay(null, function(sId, oImg, oParams) {
		$.post('/admin/media_ajax/get_image_resize', {}, function(data) {
		
			oResizeData.width = Math.round($('#edit_media_image').width() * oRatio.ratio);
			oResizeData.height = Math.round($('#edit_media_image').height() * oRatio.ratio);
			
			$('#edit_media_tool_cont').html(data.sContent);
			
			get_ratio();
			
			$('#image_resize_slider').slider({
				value: 50,
				orientation: 'horizontal', 
				slide: function(e, ui) {
					
					var iAdjustWidth = 0;
					var iAdjustHeight = 0;
					
					if (ui.value > 0) {
						
						var iAdjust = ((ui.value) * 2);
						
						
						
						iAdjustWidth = Math.round(oResizeData.width * (iAdjust / 100));
						iAdjustHeight = Math.round(oResizeData.height * (iAdjust / 100));
					}
					
					$('#image_resize_width').val(iAdjustWidth);
					$('#image_resize_height').val(iAdjustHeight);
					
					set_zoom_level(iAdjustWidth, iAdjustHeight);
				},
				change: function(e, ui) {
					$('#image_resize_apply').show();
				}
			});
			
			
			
			// bind key up events to width and height elements
			$('#image_resize_width').bind('keyup', function(e) {
				var iWidth = $(this).val();
				
				if (!isNaN(iWidth)) {
					
					var iHeight = $('#image_resize_height').val();
					
					if ($('#image_resize_maintain_ratio:checked').length === 1) {
						iHeight = Math.round(oResizeData.height * (iWidth / oResizeData.width));
					}
					

					$('#image_resize_height').val(iHeight);
				
					set_zoom_level(iWidth, iHeight);
					
					$('#image_resize_apply').show();	
				} else {
					$('#image_resize_apply').hide();
				}
			});
			
			
			
			
			// bind key up events to width and height elements
			$('#image_resize_height').bind('keyup', function(e) {
				var iHeight = $(this).val();
				
				if (!isNaN(iHeight)) {
					
					var iWidth = $('#image_resize_width').val();
					
					if ($('#image_resize_maintain_ratio:checked').length === 1) {
						iWidth = Math.round(oResizeData.width * (iHeight / oResizeData.height));
					}
					

					$('#image_resize_width').val(iWidth);
				
					set_zoom_level(iWidth, iHeight);
					
					$('#image_resize_apply').show();	
				} else {
					$('#image_resize_apply').hide();
				}
			});


			var iImageWidth = Math.round($('#edit_media_image').width() * oRatio.ratio);
			var iImageHeight = Math.round($('#edit_media_image').height() * oRatio.ratio);

			$('#image_resize_width').val(iImageWidth);
			$('#image_resize_height').val(iImageHeight);

			
			$(oImg).remove();
			$('#' + sId).remove();
		}, 'json');
	});
}



function apply_resize() {
	create_overlay(null, function(sId, oImg, oPassThru) {
	
	
		var oParams = {
				sSrcImage : strip_domain($('#edit_media_image').attr('src')),
				iMediaId : oMedia.iMediaId,
				iWidth : $('#image_resize_width').val(),
				iHeight : $('#image_resize_height').val()
		};

	
		$.post('/admin/media_ajax/apply_resize', oParams, function(data) {
		
		
			if (data.sError) {
				$.prompt(data.sError);
				$(oImg).remove();
				$('#' + sId).remove();
				return;
			}
			
			append_history({ 
				sAction : data.sHistory,
				sImgSrc : data.sContent,
				iImgWidth : oParams.iWidth,
				iImgHeight : oParams.iHeight
			});
			
			$('#edit_media_image').attr('src', data.sContent).css({
				width : oParams.iWidth + 'px',
				height : oParams.iHeight + 'px'
			});
			
			
			set_zoom_level();
			publish_history();
			
			
			$(oImg).remove();
			$('#' + sId).remove();
			
		}, 'json');
	}, {});
}